package com.example.dao.impl;

import com.example.dao.CustomDao;
import com.example.entity.customerAdmin.Custom;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomDaoImpl implements CustomDao {
    @Override
    public List<Custom> selectCusName(String name) {
        List<Custom>customList=new ArrayList<>();
        Object[]objects=new Object[1];
        objects[0]=name;
        ResultSet resultSet = BaseDao.select("select *from custom where cus_name=?", objects);
        try {
            while (resultSet.next()){
                Custom custom=new Custom();
                custom.setId(resultSet.getInt("id"));
                custom.setName(resultSet.getString("cus_name"));
                custom.setCusType(resultSet.getString("cus_type"));
                custom.setCusStar(resultSet.getString("cus_star"));
                custom.setCusState(resultSet.getString("cus_state"));
                custom.setHelpName(resultSet.getString("help_name"));
                custom.setBelIndustry(resultSet.getString("bel_industy"));
                custom.setResource(resultSet.getString("source"));
                custom.setContactId(resultSet.getInt("contactId"));
                customList.add(custom);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return customList;
    }
    @Override
    public Custom selectCusById(int contactId) {
        Object[]objects=new Object[1];
        objects[0]=contactId;
        ResultSet resultSet = BaseDao.select("select *from custom where id=?", objects);
        try {
            while (resultSet.next()){
                Custom custom=new Custom();
                custom.setId(resultSet.getInt("id"));
                custom.setName(resultSet.getString("cus_name"));
                custom.setCusType(resultSet.getString("cus_type"));
                custom.setCusStar(resultSet.getString("cus_star"));
                custom.setCusState(resultSet.getString("cus_state"));
                custom.setHelpName(resultSet.getString("help_name"));
                custom.setBelIndustry(resultSet.getString("bel_industy"));
                custom.setResource(resultSet.getString("source"));
                custom.setPid(resultSet.getInt("pid"));
                custom.setContactId(resultSet.getInt("contactId"));
                return custom;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }


    @Override
    public List<Custom> selectBelCus(int pid) {
        List<Custom>customList=new ArrayList<>();
        Object[]objects=new Object[1];
        objects[0]=pid;
        ResultSet resultSet = BaseDao.select("select *from custom where pid=?", objects);
        try {
            while (resultSet.next()){
                Custom custom=new Custom();
                custom.setId(resultSet.getInt("id"));
                custom.setName(resultSet.getString("cus_name"));
                custom.setCusType(resultSet.getString("cus_type"));
                custom.setCusStar(resultSet.getString("cus_star"));
                custom.setCusState(resultSet.getString("cus_state"));
                custom.setHelpName(resultSet.getString("help_name"));
                custom.setBelIndustry(resultSet.getString("bel_industy"));
                custom.setResource(resultSet.getString("source"));
                custom.setPid(resultSet.getInt("pid"));
                custom.setContactId(resultSet.getInt("contactId"));
                customList.add(custom);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return customList;
    }

    @Override
    public int add(Custom custom) {
        Object[]objects=new Object[9];
        objects[0]=custom.getName() ;
        objects[1]=custom.getContactId() ;
        objects[2]=custom.getCusType() ;
        objects[3]=custom.getCusStar() ;
        objects[4]=custom.getCusState() ;
        objects[5]=custom.getHelpName() ;
        objects[6]=custom.getBelIndustry() ;
        objects[7]=custom.getResource() ;
        objects[8]=custom.getPid() ;
        return BaseDao.modify("insert into custom(cus_name,contactId,cus_type,cus_star,cus_state,help_name,bel_industy,source,pid)values(?,?,?,?,?,?,?,?,?)", objects);
    }

    @Override
    public List<Custom> selectAll() {
        List<Custom>customList=new ArrayList<>();
        ResultSet resultSet = BaseDao.select("select *from custom", null);
        try {
            while (resultSet.next()){
                Custom custom=new Custom();
                custom.setId(resultSet.getInt("id"));
                custom.setName(resultSet.getString("cus_name"));
                custom.setCusType(resultSet.getString("cus_type"));
                custom.setCusStar(resultSet.getString("cus_star"));
                custom.setCusState(resultSet.getString("cus_state"));
                custom.setHelpName(resultSet.getString("help_name"));
                custom.setBelIndustry(resultSet.getString("bel_industy"));
                custom.setResource(resultSet.getString("source"));
                custom.setPid(resultSet.getInt("pid"));
                custom.setContactId(resultSet.getInt("contactId"));
                customList.add(custom);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return customList;
    }
}
